//
//  GYNetWorkTools.m
//  LockManagement
//
//  Created by apple on 2018/7/25.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

//static NSString *baseURLString = @"https://youwei.yunhongkg.com/appapi/";//https://youwei.yunhongkg.com

#import "GYNetWorkTools.h"
#import <AFNetworking.h>
#import "GYUserInfo.h"

@implementation GYNetWorkTools

/**
 创建一个请求对象
 
 @return <#return value description#>
 */
+ (AFHTTPSessionManager *)Manager {//http://119.29.88.192:8080
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:baseURLString]];
    //[manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
    [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
    //[manager setSecurityPolicy:[self customSecurityPolicy]];
    
    return manager;
}

+ (AFSecurityPolicy*)customSecurityPolicy
{
    // /先导入证书 (要记得把证书添加到程序中)
    NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"https" ofType:@"cer"];//证书的路径
    
    //NSLog(@"filepath ========%@",cerPath);
    NSData *certData = [NSData dataWithContentsOfFile:cerPath];
    
    // AFSSLPinningModeCertificate 使用证书验证模式
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
    
    securityPolicy.allowInvalidCertificates = YES;
    
    securityPolicy.validatesDomainName = NO;
    securityPolicy.pinnedCertificates = [NSSet setWithObject:certData];
    
    
    return securityPolicy;
}

#pragma mark -- 封装网络请求方法

+ (void)POST:(NSString *)url parameters:(id)dic Progress:(NetWorkingProgress _Nullable )progress Success:(NetWorkingSuccessBlock)successCallBack Error:(NetWorkingErrorBlock)errorCallBack {
    [[self Manager] POST:url parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
        if (progress) {
            progress(uploadProgress);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        successCallBack(dic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        errorCallBack(error);
    }];
}

+ (void)GET:(NSString *)url parameters:(id)dic Progress:(NetWorkingProgress _Nullable)progress Success:(NetWorkingSuccessBlock)successCallBack Error:(NetWorkingErrorBlock)errorCallBack {
    [[self Manager] GET:url parameters:dic progress:^(NSProgress * _Nonnull downloadProgress) {
        if (progress) {
            progress(downloadProgress);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        successCallBack(dic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        errorCallBack(error);
    }];
}

#pragma mark -- 接口实现

+ (void)requestGetCompanyListWithURL:(NSString *)url current:(NSString *)current pageSize:(NSString *)pageSize success:(NetWorkingSuccessBlock)successCallBack error:(NetWorkingErrorBlock)errorCallBack {
    NSDictionary *dic = @{@"current":current,@"pageSize":pageSize};
    [self GET:url parameters:dic Progress:nil Success:successCallBack Error:errorCallBack];
}

+ (void)requestUserLoginWithURL:(NSString *)url companyID:(NSString *)companyID userName:(NSString *)userName password:(NSString *)password success:(NetWorkingSuccessBlock)successCallBack error:(NetWorkingErrorBlock)errorCallBack {
    NSDictionary *dic = @{@"companyID":companyID,@"userName":userName,@"password":password};
    [self POST:url parameters:dic Progress:nil Success:successCallBack Error:errorCallBack];
}

+ (void)requestGetKeysListWithURL:(NSString *)url current:(NSString *)current pageSize:(NSString *)pageSize success:(NetWorkingSuccessBlock)successCallBack error:(NetWorkingErrorBlock)errorCallBack {
    NSDictionary *dic = @{@"token":[self getToken],@"current":current,@"pageSize":pageSize};
    [self GET:url parameters:dic Progress:nil Success:successCallBack Error:errorCallBack];
}

+ (void)requestGetLocksListWithURL:(NSString *)url lockName:(NSString *)lockName current:(NSString *)current pageSize:(NSString *)pageSize success:(NetWorkingSuccessBlock)successCallBack error:(NetWorkingErrorBlock)errorCallBack {
    NSDictionary *dic = @{@"token":[self getToken],@"lockName":lockName,@"current":current,@"pageSize":pageSize};
    [self GET:url parameters:dic Progress:nil Success:successCallBack Error:errorCallBack];
}

+ (void)requestLockCodeWithURL:(NSString *)url lockID:(NSString *)lockID lng:(float)lng lat:(float)lat success:(NetWorkingSuccessBlock)successCallBack error:(NetWorkingErrorBlock)errorCallBack {
    NSDictionary *dic = @{@"token":[self getToken],@"lockID":lockID,@"lng":@(lng),@"lat":@(lat)};
    [self POST:url parameters:dic Progress:nil Success:successCallBack Error:errorCallBack];
}

+ (void)requestGetTasksListWithURL:(NSString *)url lockName:(NSString *)lockName keyID:(NSString *)keyID taskType:(NSString *)taskType taskState:(NSString *)taskState success:(NetWorkingSuccessBlock)successCallBack error:(NetWorkingErrorBlock)errorCallBack {
    NSDictionary *dic = @{@"token":[self getToken],@"taskType":taskType,@"taskState":taskState,@"keyID":keyID,@"lockName":lockName};
    [self GET:url parameters:dic Progress:nil Success:successCallBack Error:errorCallBack];
}

+ (void)requestTaskApplyWithURL:(NSString *)url keyID:(NSString *)keyID lockID:(NSString *)lockID taskName:(NSString *)taskName taskStart:(NSString *)taskStart taskEnd:(NSString *)taskEnd taskType:(NSString *)taskType Withsuccess:(NetWorkingSuccessBlock)successCallBack error:(NetWorkingErrorBlock)errorCallBack {
    NSDictionary *dic = @{@"token":[self getToken],@"keyID":keyID,@"lockID":lockID,@"taskName":taskName,@"taskStart":taskStart,@"taskEnd":taskEnd,@"taskType":taskType};
    //NSLog(@"dic=========%@",dic);
    [self POST:url parameters:dic Progress:nil Success:successCallBack Error:errorCallBack];
}

+ (void)requestTaskGiveWithURL:(NSString *)url taskID:(NSString *)taskID success:(NetWorkingSuccessBlock)successCallBack error:(NetWorkingErrorBlock)errorCallBack {
    NSDictionary *dic = @{@"token":[self getToken],@"taskID":taskID};
    [self POST:url parameters:dic Progress:nil Success:successCallBack Error:errorCallBack];
}

+ (void)requestLogWithURL:(NSString *)url logTime:(NSString *)logTime logType:(NSString *)logType keyID:(NSString *)keyID lockID:(NSString *)lockID logState:(NSString *)logState success:(NetWorkingSuccessBlock)successCallBack error:(NetWorkingErrorBlock)errorCallBack {
    NSDictionary *dic = @{@"token":[self getToken],@"logTime":logTime,@"logType":logType,@"keyID":keyID,@"lockID":lockID,@"logState":logState};
    NSLog(@"dic==============%@",dic);
    [self POST:url parameters:dic Progress:nil Success:successCallBack Error:errorCallBack];
}

+ (void)requestLogListsWithURL:(NSString *)url logStart:(NSString *)logStart logEnd:(NSString *)logEnd current:(NSString *)current pageSize:(NSString *)pageSize success:(NetWorkingSuccessBlock)successCallBack error:(NetWorkingErrorBlock)errorCallBack {
    NSDictionary *dic = @{@"token":[self getToken],@"logStart":logStart,@"logEnd":logEnd,@"current":current,@"pageSize":pageSize};
    [self GET:url parameters:dic Progress:nil Success:successCallBack Error:errorCallBack];
}

+ (void)requestModflyUserPwdWithURL:(NSString *)url newPassword:(NSString *)newPassword success:(NetWorkingSuccessBlock)successCallBack error:(NetWorkingErrorBlock)errorCallBack {
    NSDictionary *dic = @{@"token":[self getToken],@"password":newPassword};
    [self POST:url parameters:dic Progress:nil Success:successCallBack Error:errorCallBack];
}

+ (void)requestUserLogoutWithURL:(NSString *)url success:(NetWorkingSuccessBlock)successCallBack error:(NetWorkingErrorBlock)errorCallBack {
    NSDictionary *dic = @{@"token":[self getToken]};
    [self POST:url parameters:dic Progress:nil Success:successCallBack Error:errorCallBack];
}








#pragma mark -- 得到用户登录token


+ (NSString *)getToken {
    return [[GYUserInfo loadUserInfo] token];
}

@end
